//
// Created by 12992 on 24-10-13.
//

// You may need to build the project (run Qt uic code generator) to get "ui_UploadBar.h" resolved

#include "uploadbar.h"
#include "ui_UploadBar.h"
#include <QFileDialog>

namespace gui::widgets
{
    UploadBar::UploadBar(QWidget* parent) :
            QWidget(parent), ui(new Ui::UploadBar)
    {
        ui->setupUi(this);
        this->init_openfile_button_layout();
        this->init_signal();
    }

    UploadBar::~UploadBar()
    {
        delete ui;
    }

    void UploadBar::init_openfile_button_layout()
    {
        // 创建一个水平布局
        auto* hLayout = new QHBoxLayout(this);

        // 加载图片
        QPixmap pixmap(":/icon/file.svg"); // 替换为你的图片路径
        auto* pixmapLabel = new QLabel(this);
        int scalHeight = ui->openFile->height();
        int scalWidth = pixmap.scaledToHeight(scalHeight).width(); //根据label高度计算得到控件目标宽度
        if (pixmapLabel->width() < scalWidth)
        {
            //再次调整，确保图片能够在控件中完整显示
            scalWidth = pixmapLabel->width();
            scalHeight = pixmap.scaledToWidth(scalWidth).height();
        }
        pixmapLabel->setPixmap(pixmap.scaled(scalWidth, scalHeight, Qt::KeepAspectRatio));
        pixmapLabel->setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
        // 创建一个垂直布局来放置文字
        auto* vLayout = new QVBoxLayout(this);

        // 创建两个标签来显示文字
        auto* textLabel1 = new QLabel("文件", this);
        auto* textLabel2 = new QLabel("打开视频文件", this);

        // 将文字标签添加到垂直布局
        vLayout->addWidget(textLabel1);
        vLayout->addWidget(textLabel2);

        // 将图片和文字布局添加到水平布局
        hLayout->addWidget(pixmapLabel);
        hLayout->addLayout(vLayout);

        // 将水平布局设置到按钮上
        ui->openFile->setLayout(hLayout);
    }

    void UploadBar::init_signal()
    {
        connect(this->ui->openFile, &QPushButton::clicked, this, &UploadBar::selectMediaFile);
        connect(this->ui->cancel, &QPushButton::clicked, this, &UploadBar::canceled);
    }

    void UploadBar::selectMediaFile()
    {
        auto path = QFileDialog::getOpenFileName(this, "打开媒体文件", ".", tr("Video Files (*.mp4 *.mkv *.avi)"));
        qDebug() << "open path : " << path;
        this->fileUploaded(path);
    }
} // gui::widgets
